//
//  Timer.cpp
//  RayTracer
//
//  Created by 俞云康 on 3/25/16.
//  Copyright © 2016 yushroom. All rights reserved.
//

#include "Timer.hpp"
#include "Log.hpp"

Timer::Timer(string label)
    : _label(label)
{
}

void Timer::set_label(string label)
{
    _label = label;
}

void Timer::reset()
{
    _has_started = false;
}

void Timer::begin()
{
    _start = std::chrono::high_resolution_clock::now();
    _has_started = true;
}
void Timer::end()
{
    if (!_has_started) {
        error("timer %s not started.", _label.c_str());
    }
    _end = std::chrono::high_resolution_clock::now();
    //_duration = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - _start).count();
}

void Timer::print() const
{
    //info("Time info for [%s]: ", _label.c_str());
    auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - _start).count();
    if (ms >= 1000)
        info("Time info for [%s]: %f s\n", _label.c_str(), float(ms) / 1000);
    else
        info("Time info for [%s]: %lld ms\n", _label.c_str(), ms);
}
//friend std::ostream& operator<<(std::ostream& os, const Timer& timer) {
//	log("Time info for [%s]:\n", timer.label);
//	os << "Time info for [" << label << "]:"
//}